#pragma once
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <fstream>
#include <cassert>
#include "../Comm/Log.hpp"
#include "../Comm/Util.hpp"
#include <mysql/mysql.h>
namespace ns_model
{
    using namespace std;
    using namespace ns_util;
    using namespace ns_log;
    struct Question
    {
        std::string number; // 题目编号
        std::string title;  // 题目名称
        std::string star;   // 题目难度
        std::string desc;   // 题目描述
        std::string header; // 预设置的代码段
        std::string tail;   // 测试用的代码段
        int cpu_limit;      // 时间限制
        int memo_limit;     // 空间限制
    };

    const string table_name = "oj_questions";
    const string host = "127.0.0.1";
    const string user = "magician";
    const string passwd = "8456852";
    const string db = "oj";
    const int port = 3306;
    class Model
    {
    public:
        Model() {}
        ~Model() {}

        bool QueryMySql(const string &sql, vector<Question> *out)
        {
            // 连接数据库
            MYSQL *my = mysql_init(nullptr);
            if (nullptr == mysql_real_connect(my, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0))
            {
                LOG(FATAL) << "数据量连接失败！"
                           << "\n";
                return false;
            }
            mysql_set_character_set(my, "utf8");
            LOG(INFO) << "数据量连接成功！"
                      << "\n";

            // 执行sql语句
            if (0 != mysql_query(my, sql.c_str()))
            {
                LOG(WARNING) << sql << " excute error!\n";
                return false;
            }
            // 获取结果
            MYSQL_RES *res = mysql_store_result(my);
            int n = mysql_num_rows(res);
            int m = mysql_num_fields(res);
            struct Question q;
            for (int i = 0; i < n; i++)
            {
                MYSQL_ROW row = mysql_fetch_row(res);    
                q.number = row[0];
                q.title = row[1];
                q.star = row[2];
                q.desc = row[3];
                q.header = row[4];
                q.tail = row[5];
                q.cpu_limit = stoi(row[6]);
                q.memo_limit = stoi(row[7]);
                out->push_back(q);
            }
            // 释放空间
            mysql_free_result(res);
            // 断开连接
            mysql_close(my);
            return true;
        }

        bool GetAllQuestions(vector<Question> *out)
        {
            string sql = "select * from ";
            sql += table_name;
            return QueryMySql(sql, out);
        }

        bool GetOneQuestion(const string &number, Question *out)
        {
            string sql = "select * from ";
            sql += table_name;
            sql += " where number=";
            sql += number;
            vector<Question> res;
            if (QueryMySql(sql, &res))
            {
                if (res.size() == 1)
                {
                    *out = res[0];
                    return true;
                }
            }
            return false;
        }
    };
}
